package com.floreantpos.bo.ui.explorer;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import com.floreantpos.POSConstants;
import com.floreantpos.main.Application;
import com.floreantpos.model.StoreLoss;
import com.floreantpos.model.dao.StoreLossDAO;
import com.floreantpos.swing.MessageDialog;
import com.floreantpos.swing.TransparentPanel;
import com.floreantpos.ui.PosTableRenderer;
import com.floreantpos.ui.dialog.BeanEditorDialog;
import com.floreantpos.ui.dialog.ConfirmDeleteDialog;
import com.floreantpos.ui.model.StoreLossForm;

public class StoreLossExplorer extends TransparentPanel {
	private List<StoreLoss> storeLossList;
	
	private JTable table;

	private StoreLossExplorerTableModel tableModel;
	
	public StoreLossExplorer(){
		this(POSConstants.ADMIN);
	}
	
	public StoreLossExplorer(String role) {
		this.setName(com.floreantpos.POSConstants.STORE_LOSS);
		Application.getInstance().setLastCommand(role);
		
		if(role != null && role.equals(POSConstants.ADMIN)){
			StoreLossDAO dao = new StoreLossDAO();
			storeLossList = dao.findAll();
		}else if(role != null && role.equals(POSConstants.SERVER)){
			StoreLossDAO dao = new StoreLossDAO();
			storeLossList = dao.findAllByToday();
		}
		
		tableModel = new StoreLossExplorerTableModel();
		table = new JTable(tableModel);
		table.setDefaultRenderer(Object.class, new PosTableRenderer());
		
		setLayout(new BorderLayout(5,5));
		add(new JScrollPane(table));
		
		JButton addButton = new JButton(com.floreantpos.POSConstants.ADD);
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					StoreLossForm editor = new StoreLossForm();
					
					boolean enableDate = false;
					JFrame frame;
					if(Application.getInstance().getLastCommand() != null 
							&& Application.getInstance().getLastCommand().equals(POSConstants.SERVER)){
						frame = Application.getInstance().getManagementWindow(POSConstants.STORE_LOSS);
						enableDate = false;
					}else{
						frame = Application.getInstance().getBackOfficeWindow();
						enableDate = true;
					}
					editor.enableModifiedTime(enableDate);
					
					BeanEditorDialog dialog = new BeanEditorDialog(editor, frame, true);
					dialog.open();
					if (dialog.isCanceled())
						return;
					StoreLoss storeLoss = (StoreLoss) editor.getBean();
					tableModel.addStoreLoss(storeLoss);
				} catch (Exception x) {
					x.printStackTrace();
					MessageDialog.showError(com.floreantpos.POSConstants.ERROR_MESSAGE, x);
				}
			}
			
		});
		
		JButton editButton = new JButton(com.floreantpos.POSConstants.EDIT);
		editButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					int index = table.getSelectedRow();
					if (index < 0)
						return;

					StoreLoss storeLoss = storeLossList.get(index);

					StoreLossForm editor = new StoreLossForm(storeLoss);
					
					boolean enableDate = false;
					JFrame frame;
					if(Application.getInstance().getLastCommand() != null 
							&& Application.getInstance().getLastCommand().equals(POSConstants.SERVER)){
						frame = Application.getInstance().getManagementWindow(POSConstants.STORE_LOSS);
						enableDate = false;
					}else{
						frame = Application.getInstance().getBackOfficeWindow();
						enableDate = true;
					}
					editor.enableModifiedTime(enableDate);
					
					BeanEditorDialog dialog = new BeanEditorDialog(editor, frame, true);
					dialog.open();
					if (dialog.isCanceled())
						return;

					table.repaint();
				} catch (Throwable x) {
					MessageDialog.showError(com.floreantpos.POSConstants.ERROR_MESSAGE, x);
				}
			}
			
		});
		JButton deleteButton = new JButton(com.floreantpos.POSConstants.DELETE);
		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					int index = table.getSelectedRow();
					if (index < 0)
						return;

					if (ConfirmDeleteDialog.showMessage(StoreLossExplorer.this, com.floreantpos.POSConstants.CONFIRM_DELETE, com.floreantpos.POSConstants.DELETE) == ConfirmDeleteDialog.YES) {
						StoreLoss storeLoss = storeLossList.get(index);
						StoreLossDAO dao = new StoreLossDAO();
						dao.delete(storeLoss);
						tableModel.deleteStoreLoss(storeLoss, index);
					}
				} catch (Exception x) {
					MessageDialog.showError(com.floreantpos.POSConstants.ERROR_MESSAGE, x);
				}
			}
			
		});

		TransparentPanel panel = new TransparentPanel();
		panel.add(addButton);
		panel.add(editButton);
		if(role != null && role.equals(POSConstants.ADMIN)){
			panel.add(deleteButton);
		}
		add(panel, BorderLayout.SOUTH);
	}
	
	class StoreLossExplorerTableModel extends AbstractTableModel {
		String[] columnNames = {com.floreantpos.POSConstants.ID, com.floreantpos.POSConstants.NAME, com.floreantpos.POSConstants.STORE_REST_NUMBER, com.floreantpos.POSConstants.DATE, com.floreantpos.POSConstants.USER};
		
		public int getRowCount() {
			if(storeLossList == null) {
				return 0;
			}
			return storeLossList.size();
		}

		public int getColumnCount() {
			return columnNames.length;
		}
		
		@Override
		public String getColumnName(int column) {
			return columnNames[column];
		}
		
		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return false;
		}

		public Object getValueAt(int rowIndex, int columnIndex) {
			if(storeLossList == null)
				return "";
			
			StoreLoss storeLoss = storeLossList.get(rowIndex);
			
			switch(columnIndex) {
				case 0:
					return String.valueOf(storeLoss.getId());
					
				case 1:
					return storeLoss.getStoreName();
					
				case 2:
					return storeLoss.getNumbers();

				case 3:
					return storeLoss.getModifiedTime();
					
				case 4:
					return (storeLoss.getUser()!=null?storeLoss.getUser().getFirstName():"");

			}
			return null;
		}

		public void addStoreLoss(StoreLoss storeLoss) {
			int size = storeLossList.size();
			storeLossList.add(storeLoss);
			fireTableRowsInserted(size, size);
		}
		
		public void deleteStoreLoss(StoreLoss storeLoss, int index) {
			storeLossList.remove(storeLoss);
			fireTableRowsDeleted(index, index);
		}
	}
}
